package com.demo.test.controller;

import com.demo.test.entity.*;
import com.demo.test.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author XieHuan
 * @since 2017-06-12
 */
@Controller
@RequestMapping("/battle")
public class BattleController {

    @Autowired
    private IBattleService iBattleService;
    @Autowired
    private ISingService iSingService;
    @Autowired
    private ITongjiService iTongjiService;
    @Autowired
    private IToupiaoService iToupiaoService;
    @Autowired
    private IWinnerService iWinnerService;

    @RequestMapping("/allbattles")
    @ResponseBody
    public List<Battle> allBattle(){
        return  iBattleService.selectList(null);

    }

    @RequestMapping("/addBattles")
    @ResponseBody
    public boolean addBattle(Battle battle){
        Sing firstSing=iSingService.selectById(battle.getFirstSingid());

        Sing secondSing=iSingService.selectById(battle.getSecondSingid());

        battle.setFirstSingname(firstSing.getSingname());

        battle.setSecondSingname(secondSing.getSingname());

        boolean t=iBattleService.insert(battle);

        return  true;
    }

    @RequestMapping("/updateSingShutdoun")
    @ResponseBody
    public boolean updateSingShutdoun(@RequestBody Battle  battle){
        battle.setType("0");

        boolean t=iBattleService.updateById(battle);

        return t;
    }

    @RequestMapping("/updateBattleZhongzhi")
    @ResponseBody
    public  boolean updateBattleZhongzhi(@RequestBody Battle battle){

        battle.setType("2");

        boolean t=iBattleService.updateById(battle);
        if(t==true){

            SingVo s1=iSingService.selectSingPiaoById(Integer.parseInt(battle.getFirstSingid()));
            SingVo s2=iSingService.selectSingPiaoById(Integer.parseInt(battle.getSecondSingid()));
            Tongji tongji=new Tongji();
            Winner winner=new Winner();
            tongji.setBattleid(battle.getId()+"");
            tongji.setFirstSingid(battle.getFirstSingid());
            tongji.setFirstSinpiao(s1.getNumber());
            tongji.setSecondSingid(battle.getSecondSingid());
            tongji.setSecondSingpiao(s2.getNumber());
            int x=iToupiaoService.deleteBySingid(battle.getFirstSingid());
            int y=iToupiaoService.deleteBySingid(battle.getSecondSingid());

            if(Integer.parseInt(s1.getNumber())>Integer.parseInt(s2.getNumber())){
                winner.setBattleid(battle.getId()+"");
                winner.setWinnerid(battle.getFirstSingid());
                winner.setWinnerpiao(s1.getNumber());

                boolean z=iWinnerService.insert(winner);


            }else if (Integer.parseInt(s1.getNumber())==Integer.parseInt(s2.getNumber())){
                winner.setBattleid(battle.getId()+"");
                winner.setWinnerid(battle.getFirstSingid());
                winner.setWinnerpiao(s1.getNumber());
                boolean z=iWinnerService.insert(winner);
                Winner winner1=new Winner();
                winner1.setBattleid(battle.getId()+"");
                winner1.setWinnerid(battle.getSecondSingid());
                winner1.setWinnerpiao(s2.getNumber());
                boolean z1=iWinnerService.insert(winner1);

            }else{

                winner.setBattleid(battle.getId()+"");
                winner.setWinnerid(battle.getSecondSingid());
                winner.setWinnerpiao(s2.getNumber());
                boolean z1=iWinnerService.insert(winner);
            }




            boolean f=iTongjiService.insert(tongji);
            if (f==true&&x>0&&y>0){
                return  true;
            }
             return   false;

        }else {
            return false;
        }
    }

    @RequestMapping("/updateSingOpen")
    @ResponseBody
    public boolean updateSingOpen(@RequestBody Battle battle){
        battle.setType("1");

        boolean t=iBattleService.updateById(battle);
        return t;
    }
    //根据对战信息，返回选手信息
    @RequestMapping("/selectAllSingsForBattle")
    @ResponseBody
    public List<Sing> selectAllBattleByType1(){

        List<Battle> battleList=iBattleService.selectAllBattleByType1();


        if(battleList.size()==1){
            List<Sing> singList=new ArrayList<Sing>();
            Sing s1=new Sing();
            Sing  s2=new Sing();
            s1.setId(Integer.parseInt(battleList.get(0).getFirstSingid()));
            s1.setSingname(battleList.get(0).getFirstSingname());
            s2.setId(Integer.parseInt(battleList.get(0).getSecondSingid()));
            s2.setSingname(battleList.get(0).getSecondSingname());

            singList.add(s1);
            singList.add(s2);

            return  singList;

        }else{
            return  null;
        }


    }


    //根据对战信息，查询出选手得票信息

    @RequestMapping("/selectSingVoByBattle")
    @ResponseBody
    public List<SingVo> selectSingVoByBattle(){
        List<Battle> battleList=iBattleService.selectAllBattleByType1();

        if (battleList.size()==1){
            List<SingVo> singVoList=new ArrayList<SingVo>();
            SingVo s1=iSingService.selectSingPiaoById(Integer.parseInt(battleList.get(0).getFirstSingid()));
            SingVo s2=iSingService.selectSingPiaoById(Integer.parseInt(battleList.get(0).getSecondSingid()));
            singVoList.add(s1);
            singVoList.add(s2);
            return singVoList;

        }else {
            return  null;
        }
    }


	
}
